package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCombLeaderData;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserCombLeaderDaoImpl extends TempDataObjectDao<UserCombLeaderData> {

	public UserCombLeaderData getUserCombLeaderDataByPatronsId(long userId, int patronsId) {
		String sql = "select * from `t_u_comb_leader` where `userId`=? and leaderId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			int i = 1;
			call.setLong(i++, userId);
			call.setInt(i++, patronsId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserCombLeaderData data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	public static String getUserCombLeaderDataSql = "select * from `t_u_comb_leader` where `userId`=?";
	public Map<Integer, UserCombLeaderData> getUserCombLeaderData(long userId) {
		Map<Integer, UserCombLeaderData> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserCombLeaderDataSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserCombLeaderData data = parse(rs);
				dataMap.put(data.getCombId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	/**
	 * 只读leaderId有值的,并且leaderId为key
	 * @param userId
	 * @return
	 */
	public Map<Integer, UserCombLeaderData> getUserCombLeaderDataByPatronsKey(long userId) {
		Map<Integer, UserCombLeaderData> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserCombLeaderDataSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserCombLeaderData data = parse(rs);
				if (data.getLeaderId() > 0) {
					dataMap.put(data.getLeaderId(), data);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}


	public static String updateUserCombLeaderDataSql = "update `t_u_comb_leader` set leaderId=?,promoteId=?,lastChooseTime=?,keepSakePatronsId=?,keepSakeTime=?,skillMap=? where userId=? and combId=?";
	@Override
	protected boolean updateData(UserCombLeaderData userCombLeaderData) {
		userCombLeaderData.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getLeaderId()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getPromoteId()));
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getLastChooseTime()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getKeepSakePatronsId()));
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getKeepSakeTime()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userCombLeaderData.getSkillMap())));
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getCombId()));
		if (getDb().executeNoneQuery(updateUserCombLeaderDataSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserCombLeaderDataSql = "insert into `t_u_comb_leader`(userId,combId,leaderId,promoteId,lastChooseTime,keepSakePatronsId,keepSakeTime,skillMap) values (?,?,?,?,?,?,?,?)";
	@Override
	protected boolean addData(UserCombLeaderData userCombLeaderData) {
		userCombLeaderData.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getCombId()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getLeaderId()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getPromoteId()));
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getLastChooseTime()));
		para.put(i++, new DbParameter(Types.INTEGER, userCombLeaderData.getKeepSakePatronsId()));
		para.put(i++, new DbParameter(Types.BIGINT, userCombLeaderData.getKeepSakeTime()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userCombLeaderData.getSkillMap())));
		if (getDb().executeNoneQuery(insertUserCombLeaderDataSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserCombLeaderData parseData(ResultSet rs) throws SQLException {
		UserCombLeaderData userCombLeaderData = new UserCombLeaderData();
		userCombLeaderData.setUserId(rs.getLong("userId"));
		userCombLeaderData.setCombId(rs.getInt("combId"));
		userCombLeaderData.setLeaderId(rs.getInt("leaderId"));
		userCombLeaderData.setPromoteId(rs.getInt("promoteId"));
		userCombLeaderData.setLastChooseTime(rs.getLong("lastChooseTime"));
		userCombLeaderData.setKeepSakePatronsId(rs.getInt("keepSakePatronsId"));
		userCombLeaderData.setKeepSakeTime(rs.getInt("keepSakeTime"));
		userCombLeaderData.setSkillMap(JSON.parseObject(rs.getString("skillMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.player.CombLeaderSkill>>() {}));

		return userCombLeaderData;
	}
}
